package com.c.ctgproject.ui.files

import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.file.FileManagerAdapter
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.file.FileFormalitiesManagerEntity
import com.c.ctgproject.utils.*
import com.c.ctgproject.wiget.dialog.FileManagerMoreDialog
import com.c.ctgproject.wiget.dialog.FileManagerMoreViewModel
import com.c.ctgproject.wiget.dialog.FileSendMoreFormalitiesDialog
import kotlinx.android.synthetic.main.activity_file_manager.*
import org.jetbrains.anko.startActivity

/**
 *作者:LiBW
 *创建日期: 2021/7/5
 *描述: 档案管理 页面(列表) 手续管理 页面
 */
class FileManagerActivity : BaseActivity(), FileManagerMoreDialog.FileManagerMoreCallback, FileManagerAdapter.FileManagerAdapterCallback, FileSendMoreFormalitiesDialog.FileSendMoreFormalitiesCallback {
    private val mViewModel by lazy { ViewModelProvider(this)[FileManagerViewModel::class.java] }
    private val mButtonViewModel by lazy { ViewModelProvider(this)[FileManagerMoreViewModel::class.java] }
    private var search = ""
    private var page = 1

    //主数据
    private var mList = ArrayList<FileFormalitiesManagerEntity.DataBean.ListBean>()
    private val mAdapter by lazy { FileManagerAdapter(this, mList, this) }

    //弹窗
    private var changeMoreDialog: FileManagerMoreDialog? = null
    private var sendMoreDialog: FileSendMoreFormalitiesDialog? = null
    override fun getLayout() = R.layout.activity_file_manager
    override fun onResume() {
        super.onResume()
        getData(true)
    }

    fun getData(newData: Boolean = false) {
        loading.show()
        if (newData) {
            mList.clear()
            page = 1
        }
        mViewModel.getList(search, page)
    }

    override fun event() {
        mRecyclerView.adapter = mAdapter
        mRecyclerView.layoutManager = LinearLayoutManager(this)

        mViewModel.error.observe(this, {
            mRefresh.over()
            loading.dismiss()
            MyLog.d("错误:$it")
            showToast(it)
        })

        mViewModel.result.observe(this, {
            loading.dismiss()
            mRefresh.over()
            MyLog.d("档案管理列表:$it")
            handlerHttpResult(it) {
                val data = getGson().fromJson(it, FileFormalitiesManagerEntity::class.java).data
                data?.list?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                }
            }
        })
        mButtonViewModel.result.observe(this, {
            MyLog.d("提交回调:$it")
            handlerHttpResult(it) {
                showToast("提交成功")
                getData(true)
            }
        })
    }

    override fun click() {
        mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }
        mRefresh.setOnRefreshListener {
            getData(true)
        }

        val selectList = ArrayList<String>()

        //批量管理
        mManagerMoreTV.setOnClickListener {
            selectList.clear()
            mList.forEach { bean ->
                if (bean.select) {
                    bean.carId?.let { selectList.add(it) }
                }
            }
            if (selectList.isEmpty()) {
                showToast("请至少选择一条进行操作")
            } else {
                changeMoreDialog = FileManagerMoreDialog(selectList, this)
                changeMoreDialog?.show(supportFragmentManager, "")
            }
        }

        //搜索
        mSearchTV.setOnClickListener {
            val str = mCarNumberET.text.handlerNull()
            if (str.isNotEmpty()) {
                search = str
                getData(true)
            }
        }

        //清空输入框
        mCleanIV.setOnClickListener {
            mCarNumberET.textSet("")
        }
        mCarNumberET.handlerText {
            if (it.isEmpty()) {
                search = ""
                getData(true)
            }
        }
    }

    //批量处理结束
    override fun onFinish() {
        showToast("提交成功")
        changeMoreDialog?.dismiss()
        getData(true)
    }

    //批量手续发放
    override fun onBatchGiveProcedure(selectList: List<String>) {
        changeMoreDialog?.dismiss()
        sendMoreDialog = FileSendMoreFormalitiesDialog(selectList, this)
        sendMoreDialog?.show(supportFragmentManager, "")
    }

    //复选框点击
    override fun onCheckChange(position: Int, isCheck: Boolean) {
        mList[position].select = isCheck
        mAdapter.notifyDataSetChanged()
    }

    //回收证明
    override fun onItemReceive(position: Int) {
        mButtonViewModel.batchGiveRecycleProof(arrayListOf(mList[position].carId.handlerNull()))
    }

    //注销办理
    override fun onItemCancel(position: Int) {
        mButtonViewModel.batchGiveCancellationProof(arrayListOf(mList[position].carId.handlerNull()))
    }

    override fun onManager(position: Int) {
        //去详情
        startActivity<FileManagerDetailActivity>(
            MyConstants.CAR_ID to mList[position].carId,
            MyConstants.DATA to mList[position]
        )
    }

    override fun sendFormalities(position: Int) {
        sendMoreDialog = FileSendMoreFormalitiesDialog(arrayListOf(mList[position].carId.handlerNull()), this)
        sendMoreDialog?.show(supportFragmentManager, "")
    }

    override fun onDialogDismiss() {
        getData(true)
        sendMoreDialog?.dismiss()
    }
}